package alscript.lua.bridge.util.app;



import android.util.Log;

import com.imy.luasupport.core.LuaCore;
import com.imy.luasupport.util.AppUtils;
import com.virtual.common.utils.ToastUtil;

import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;

import alscript.lua.bridge.util.BaseFunLib;
import alscript.lua.bridge.util.LibRegister;

@LibRegister(libName = "app")
public class AppLib extends BaseFunLib {
  @Override
  protected void addFunction(LuaTable lib) {
    lib.set("launch", new Launch());
    lib.set("getPackageName", new GetPackageName());
    lib.set("toast",new Toast());
  }

  static final class  Toast extends OneArgFunction{
    @Override
    public LuaValue call(LuaValue arg) {
      String msg = arg.checkjstring();
      ToastUtil.toast(LuaCore.get().getContext(),msg);
      return LuaValue.valueOf(0);
    }
  }
  static final class GetPackageName extends OneArgFunction {

    @Override
    public LuaValue call(LuaValue arg) {
      String checkjstring = arg.checkjstring();
      String packageNameByName = AppUtils.getPackageNameByName(checkjstring);
      return LuaValue.valueOf(packageNameByName);
    }
  }

  static final class Launch extends OneArgFunction {

    @Override
    public LuaValue call(LuaValue arg) {
      String packageName = arg.checkjstring();

      return LuaValue.valueOf(LuaCore.get().getCallVirtualListener().launchApp(packageName));
    }
  }
}
